package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.ActivityRecord;
import com.example.service.ActivityRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "活动现场管理")
@RestController
@RequestMapping("/api/activity/record")
@RequiredArgsConstructor
public class ActivityRecordController {

    private final ActivityRecordService activityRecordService;

    @Operation(summary = "志愿者签到")
    @PostMapping("/sign-in")
    public Result<Boolean> signIn(
            @Parameter(description = "活动ID") @RequestParam Long activityId,
            @Parameter(description = "志愿者ID") @RequestParam Long volunteerId,
            @Parameter(description = "签到地点") @RequestParam String location) {
        return Result.success(activityRecordService.signIn(activityId, volunteerId, location));
    }

    @Operation(summary = "志愿者签退")
    @PostMapping("/sign-out")
    public Result<Boolean> signOut(
            @Parameter(description = "活动ID") @RequestParam Long activityId,
            @Parameter(description = "志愿者ID") @RequestParam Long volunteerId) {
        return Result.success(activityRecordService.signOut(activityId, volunteerId));
    }

    @Operation(summary = "补录签到记录")
    @PostMapping("/add")
    public Result<Boolean> addRecord(@RequestBody ActivityRecord record) {
        return Result.success(activityRecordService.addRecord(record));
    }

    @Operation(summary = "审核签到记录")
    @PostMapping("/review/{id}")
    public Result<Boolean> reviewRecord(
            @Parameter(description = "记录ID") @PathVariable Long id,
            @Parameter(description = "审核信息") @RequestBody Map<String, Object> reviewInfo) {
        Integer status = Integer.valueOf(reviewInfo.get("status").toString());
        String rejectReason = reviewInfo.get("rejectReason") != null ? 
                reviewInfo.get("rejectReason").toString() : null;
        Long reviewerId = Long.valueOf(reviewInfo.get("reviewerId").toString());
        return Result.success(activityRecordService.reviewRecord(id, status, rejectReason, reviewerId));
    }

    @Operation(summary = "分页查询签到记录")
    @GetMapping("/list")
    public Result<Map<String, Object>> getRecordList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "活动ID") @RequestParam(required = false) Long activityId,
            @Parameter(description = "志愿者ID") @RequestParam(required = false) Long volunteerId,
            @Parameter(description = "状态：0-待审核 1-已审核 2-已驳回") @RequestParam(required = false) Integer status,
            @Parameter(description = "类型：0-正常签到 1-补录") @RequestParam(required = false) Integer type) {
        
        Page<ActivityRecord> pageResult = activityRecordService.getRecordList(
                pageNum, pageSize, activityId, volunteerId, status, type);
        Map<String, Object> result = new HashMap<>();
        result.put("total", pageResult.getTotal());
        result.put("list", pageResult.getRecords());
        return Result.success(result);
    }

    @Operation(summary = "获取签到记录详情")
    @GetMapping("/{id}")
    public Result<ActivityRecord> getRecordInfo(
            @Parameter(description = "记录ID") @PathVariable Long id) {
        return Result.success(activityRecordService.getRecordInfo(id));
    }

    @Operation(summary = "删除签到记录")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteRecord(
            @Parameter(description = "记录ID") @PathVariable Long id) {
        return Result.success(activityRecordService.deleteRecord(id));
    }

    @Operation(summary = "批量删除签到记录")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteRecords(@RequestBody Long[] ids) {
        return Result.success(activityRecordService.batchDeleteRecords(ids));
    }
} 